#pragma once 
#include"detail.hpp"
#include"field.hpp"
#include<functional>
#include<memory>
namespace bitRPC{
    class BaseMessage{
        public:
        using ptr=std::shared_ptr<BaseMessage>;
        virtual ~BaseMessage(){};
        virtual void setId(const std::string& id){
            _rid=id;
        }
        virtual void setMType(MType type){
            _type=type;
        }
        virtual std::string Rid() {
            return _rid;
        }
        virtual MType mtype() { 
            return _type; 
        }

        virtual std::string serialize()=0;//每一个派生类根据自己的数据去实现序列化
        virtual bool unserialize(const std::string& data)=0;//针对接收到的数据进行一个解析
        virtual bool check()=0;//消息的字段是否完备


        private:
            std::string _rid;//请求id
            MType _type;//请求类型
    };
    class BaseBuffer{
        public:
            using ptr=std::shared_ptr<BaseBuffer>;
            virtual size_t readableSize() = 0;//缓冲区的长度
            virtual int32_t peekInt32() = 0;//读取四字节，不删除
            virtual void retrieveInt32() = 0;//移除前四节的数据
            virtual int32_t readInt32() = 0;//读取四字节并删除
            virtual std::string retrieveAsString(size_t len) = 0;//读取指定长度的字符串，并删除
    };
    class BaseProtocol{
        public:
        //接收到的消息
        using ptr=std::shared_ptr<BaseProtocol>;
           virtual bool onMessage(BaseBuffer::ptr& buffer,BaseMessage::ptr& msg)=0;
           virtual bool canProcess(BaseBuffer::ptr& buffer)=0;//当前缓冲区的能否处理
        //    发送的消息
           virtual std::string serialize(const BaseMessage::ptr& msg)=0;
           virtual ~BaseProtocol() = default;
    };
    class BaseConnection{
        public:
            using ptr=std::shared_ptr<BaseConnection>;
            virtual bool send(const BaseMessage::ptr& msg)=0;//发送消息
            virtual bool shutdown()=0;//关闭连接
            virtual bool connected()=0;//是否连接
    };
    // 回调函数的类型
    using ConnectionCallback=std::function<void(const BaseConnection::ptr&)>;
    using MessageCallback=std::function<void(const BaseConnection::ptr&,const BaseMessage::ptr&)>;
    using CloseCallback=std::function<void(const BaseConnection::ptr&)>;
    class BaseServer{
        public:
            using ptr=std::shared_ptr<BaseServer>;
            virtual void setConnections(const ConnectionCallback& connCallback)
            {
                _cb_connection=connCallback;
            }
            virtual void setMessage(const MessageCallback& msgCallback)
            {
                _cb_message=msgCallback;
            }
            virtual void setClose(const CloseCallback& closecallback)
            {
                _cb_close=closecallback;
            }

        protected:
            ConnectionCallback _cb_connection;
            MessageCallback _cb_message;
            CloseCallback _cb_close;
    };
    class BaseClient{
        public:
            using ptr=std::shared_ptr<BaseClient>;
    
            virtual void setConnectionsCallback(const ConnectionCallback& connCallback)
            {
                _cb_connection=connCallback;
            }
            virtual void setMessageCallback(const MessageCallback& msgCallback)
            {
                _cb_message=msgCallback;
            }
            virtual void setClosecCallback(const CloseCallback& closecallback)
            {
                _cb_close=closecallback;
            }
            virtual bool connect()=0;//连接服务器
            virtual bool shutdown()=0;
            virtual bool send(const BaseMessage::ptr& msg)=0;
            virtual BaseConnection::ptr connection()=0;//获取连接
            virtual bool connected()=0;//是否连接
        protected:
                ConnectionCallback _cb_connection;
                MessageCallback _cb_message;
                CloseCallback _cb_close;
    };

}